package _interview100;

import java.util.ArrayList;
import java.util.List;

/**
 * 46. 全排列
 */
public class No46 {
    private List<List<Integer>> ans;
    private List<Integer> path;
    private int[] nums;

    public List<List<Integer>> permute(int[] nums) {
        ans = new ArrayList<>();
        path = new ArrayList<>();
        this.nums = nums;
        dfs(nums.length);
        return ans;
    }

    private void dfs(int i) {
        if (i == 0) {
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int num : nums) {
            if (path.contains(num)) continue;
            path.add(num);
            dfs(i - 1);
            path.remove(path.size() - 1);
        }
    }
}
